package com.ddxz.tools.license;

import com.ddxz.tools.license.interceptor.DdxzWebConfigurer;
import com.ddxz.tools.license.listener.LicenseListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/6/8
 */

@Configuration
@EnableConfigurationProperties(LicenseProperties.class)
@ConditionalOnProperty(prefix = "ddxz", value = "enabled", matchIfMissing = true)
public class LicenseConfigure {
    private final LicenseProperties properties;

    public LicenseConfigure(LicenseProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean(LicenseListener.class)
    public LicenseListener licenseListener() {
        return new LicenseListener(properties.getLicense().getFile(), properties.getLicense().getName(), applicationName);
    }

    @Bean
    @ConditionalOnMissingBean(DdxzWebConfigurer.class)
    public DdxzWebConfigurer casicWebConfigurer() {
        return new DdxzWebConfigurer();
    }

    @Value("${spring.application.name}")
    private String applicationName;
}
